/*
 * Created on 2005/05/06
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;

/**
 * 
 *
 */
public class AlphaLayerFigure extends DoubleScalableFigure{
private int alpha;
private Color color;
protected Image layerImage;
private ImageData imageData;

public AlphaLayerFigure(Color color,int alpha){
    super(1,1);
    this.color=color;
    this.alpha=alpha;
    PaletteData palette=new PaletteData(new RGB[]{color.getRGB()});
    imageData = new ImageData(1,1,8,palette);
    imageData.alpha=alpha;
    imageData.setPixel(0,0,0);
    layerImage = new Image(null,imageData);   
}

public void setScale(double w,double h){
    double changew=w/wzoom;
    double changeh=h/hzoom;
   
    /*
    Point pt=getLocation();
    log.infoprev:"+pt);
    log.infochanged:"+changew+","+changeh);
    log.infoparent:"+getParent().getClientArea());
    pt.x=(int)((pt.x-getParent().getClientArea().x)*changew);
    pt.y=(int)((pt.y-getParent().getClientArea().y)*changeh);
    log.infomove:"+pt);
    setLocation(pt);
    setSize((int)(getSize().width*changew),(int)(getSize().height*changeh));
    */
    wzoom=w;
    hzoom=h;
}

public void setAlpha(int alpha){
    imageData.alpha=alpha;
}
public int getAlpha(){
    return imageData.alpha;
}
public void setColor(Color color){
  //  log.infoset color:"+color);
    this.color=color;
    imageData.palette.colors[0]=color.getRGB();
    layerImage=new Image(null,imageData);
    getParent().repaint();
}
public Color getColor(){
    return color;
}

public void paintFigure(Graphics g){
    
    //log.info(color+","+getClientArea()+","+getParent().getClientArea());
    Rectangle rectangle=getClientArea();
    //rectangle.x=rectangle.x-getParent().getClientArea().x;//???
   // rectangle.y=rectangle.y-getParent().getClientArea().y;//???
g.drawImage(
        layerImage,new Rectangle(layerImage.getBounds()),rectangle);  
}

}
